package com.zhpj.util;

import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

/**
 * 设置ini文件的工具类
 */

@Component
@PropertySource(value = "classpath:application-path.properties")
public class IniUtil {

    @Value("${INI_FILE_PATH}")
    private String iniConfigFilePath;


    /**
     * 获取token的值
     * @return
     */
    public  String getTokenValue() {
        String iniFilePath = getIniFilePath();
        try {
            Configurations configs = new Configurations();
            INIConfiguration iniConfig = configs.ini(iniFilePath);
            return iniConfig.getString("TOKEN.token");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置token的值
     * @param tokenValue
     */
    public  void setTokenValue(String tokenValue) {
        String iniFilePath = getIniFilePath();
        try {
            Configurations configs = new Configurations();
            FileBasedConfigurationBuilder<INIConfiguration> builder = configs.fileBasedBuilder(INIConfiguration.class, new File(iniFilePath));
            INIConfiguration iniConfig = builder.getConfiguration();
            iniConfig.setProperty("TOKEN.token", tokenValue);

            // 将 INI 文件以 UTF-8 编码保存
            try (FileOutputStream fos = new FileOutputStream(iniFilePath);
                 OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
                iniConfig.write(osw);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获wanset.ini配置文件的路径
     * @return
     */
    public  String getIniFilePath() {
        // 获取项目根目录的路径
        String projectRoot = System.getProperty("user.dir");
        System.out.println(projectRoot);
        ////获取最后一次 \ 出现的位置
        //int i = projectRoot.lastIndexOf("\\");
        ////截取到当前位置
        //String substring = projectRoot.substring(0,i+1);
        String path = projectRoot+"\\iniPathConfig.ini";
        try {
            Configurations configs = new Configurations();
            INIConfiguration iniConfig = configs.ini(path);
            return iniConfig.getString("PATH.path");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //public static void main(String[] args) {
    //    // 获取项目根目录的路径
    //    String projectRoot = System.getProperty("user.dir");
    //    System.out.println(projectRoot);
    //    //获取最后一次 \ 出现的位置
    //    int i = projectRoot.lastIndexOf("\\");
    //    //截取到当前位置
    //    String substring = projectRoot.substring(0,i+1);
    //    System.out.println(substring);
    //}

}